package com.cloudbroker.bcs.platform.ltb.middle.bizservice.impl;

import com.cloudbroker.bcs.common.entity.HSBCPSBackVO;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.csvc.lightinvesting.api.entity.SubscriptionCombineResult;
import com.cloudbroker.bcs.csvc.lightinvesting.api.t2service.LedT2Service;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.fansRelated.CombDetailForm;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.request.fansRelated.SubscriptionCombineForm;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.response.CombDetailQryBackVo;
import com.cloudbroker.bcs.csvc.lightinvesting.api.vo.response.SubscriptionCombineQryBackVo;
import com.cloudbroker.bcs.csvc.push.api.vo.response.PushBack;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.HSBCPSErrorCodes;
import com.cloudbroker.bcs.platform.ltb.bizapi.constants.type.UserRemindType;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.user.UserRemindBizService;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.user.back.UserRemindQryBack;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.user.back.UserRemindQryData;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.user.vo.AllRemindSaveForm;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.user.vo.RemindSaveForm;
import com.cloudbroker.bcs.platform.ltb.bizapi.service.user.vo.UserRemindQryForm;
import com.cloudbroker.bcs.platform.ltb.middle.entity.UserRemind;
import com.cloudbroker.bcs.platform.ltb.middle.service.UserRemindService;
import com.cloudbroker.bcs.platform.ltb.middle.service.UserService;
import com.cloudbroker.bcs.platform.ltb.middle.util.StringUtil;
import com.cloudbroker.bcs.platform.ltb.middle.util.UserTokenUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by daibd19671 on 2017/2/7.
 */
@Service
public class UserRemindBizServiceImpl implements UserRemindBizService {

    @Resource
    private UserService userService;
    @Resource
    private UserRemindService userRemindService;
    @Resource
    private LedT2Service ledT2Service;

    @Override
    public UserRemindQryBack user_remind_qry(UserRemindQryForm form) {
        UserRemind qry = new UserRemind();
        qry.setUser_id(form.getUser().getUser_id());
        List<UserRemind> list = userRemindService.qry(qry);
        UserRemindQryBack back = new UserRemindQryBack();
        for(UserRemind userRemind : list){
            if(userRemind.getRemind_type().equals(UserRemindType.LEADER.getVal())){
                back.setLeader_flag(userRemind.getFlag());
            }
            if(userRemind.getRemind_type().equals(UserRemindType.COMBINE.getVal())
                    && StringUtils.isEmpty(userRemind.getTag())){
                back.setCombi_flag(userRemind.getFlag());
            }
        }
        //数据为空默认关闭
        if(back.getLeader_flag() == null){
            back.setLeader_flag(0);
        }
        if(back.getCombi_flag() == null){
            back.setCombi_flag(0);
        }
        SubscriptionCombineForm subscriptionCombineForm = new SubscriptionCombineForm();
        subscriptionCombineForm.setUser_id(String.valueOf(form.getUser().getUser_id()));
        subscriptionCombineForm.setUser_token(form.getUser_token());
        SubscriptionCombineQryBackVo subscriptionBack = ledT2Service.func_comb_subscription(subscriptionCombineForm);
        if(!VOUtil.isSuccess(subscriptionBack)){
            return VOUtil.genBGErrorResult(subscriptionBack.getError_no(),subscriptionBack.getError_info(),UserRemindQryBack.class);
        }
        List<SubscriptionCombineResult> dataList = subscriptionBack.getData_list();
        List<UserRemindQryData> backList = new ArrayList<>();
        if(dataList!=null && dataList.size()!=0) {
            for (SubscriptionCombineResult result : dataList) {
                UserRemindQryData userRemindQryData = new UserRemindQryData();
                userRemindQryData.setCombi_code(result.getCombi_code());
                userRemindQryData.setCombi_name(result.getCombi_name());
                for(UserRemind userRemind : list){
                    if(userRemind.getRemind_type().equals(UserRemindType.COMBINE.getVal())
                            && userRemind.getTag()!=null && userRemind.getTag().equals(result.getCombi_code())){
                        userRemindQryData.setFlag(userRemind.getFlag());
                        break;
                    }
                }
                //数据为空默认关闭
                if(userRemindQryData.getFlag() == null){
                    userRemindQryData.setFlag(0);
                }
                backList.add(userRemindQryData);
            }
        }
        back.setData_list(backList);
        return VOUtil.setSuccessResult(back);
    }

    @Override
    public HSBCPSBackVO save_remind(RemindSaveForm form) {
        if (form.getFlag() != 0 && form.getFlag() != 1) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.PushErrorInfo.XINAN_PUSH_FLAG_ERR, PushBack.class);
        }
        if(!form.getRemind_type().equals(UserRemindType.COMBINE.getVal())
                && !form.getRemind_type().equals(UserRemindType.LEADER.getVal())){
            return VOUtil.genErrorResult(HSBCPSErrorCodes.PushErrorInfo.XINAN_PUSH_REMIND_TYPE_ERR, PushBack.class);
        }
        if(form.getRemind_type().equals(UserRemindType.COMBINE.getVal())
                && StringUtils.isEmpty(form.getCombi_code())){
            return VOUtil.genErrorResult(HSBCPSErrorCodes.PushErrorInfo.XINAN_PUSH_COMBI_CODE_EMPTY, PushBack.class);
        }
        UserRemind qry = new UserRemind();
        qry.setCompany_id(form.getCompany_id());
        qry.setUser_id(form.getUser().getUser_id());
        qry.setRemind_type(form.getRemind_type());
        if(form.getRemind_type().equals(UserRemindType.COMBINE.getVal())){
            //查询组合基本信息
            CombDetailForm combDetailForm = new CombDetailForm();
            combDetailForm.setOp_branch_no(form.getOp_branch_no());
            combDetailForm.setOp_entrust_way(form.getOp_entrust_way());
            combDetailForm.setOp_station(form.getOp_station());
            combDetailForm.setBranch_no(form.getBranch_no());
            combDetailForm.setPassword(form.getPassword());
            combDetailForm.setPassword_type(form.getPassword_type());
            combDetailForm.setUser_token(form.getUser_token());
            combDetailForm.setSendercomp_id(form.getCompany_id());
            combDetailForm.setCombi_code(form.getCombi_code());
            CombDetailQryBackVo combDetailQryBackVo = ledT2Service.func_comb_details(combDetailForm);
            if(!VOUtil.isSuccess(combDetailQryBackVo)){
                return VOUtil.genBGErrorResult(combDetailQryBackVo.getError_no(),combDetailQryBackVo.getError_info(),PushBack.class);
            }
            if(combDetailQryBackVo.getData_list()==null || combDetailQryBackVo.getData_list().size() == 0){
                return VOUtil.genErrorResult(HSBCPSErrorCodes.CombinationErrorInfo.COMBINATION_NOT_EXIST,PushBack.class);
            }
            SubscriptionCombineForm subscriptionCombineForm = new SubscriptionCombineForm();
            subscriptionCombineForm.setUser_id(String.valueOf(form.getUser().getUser_id()));
            subscriptionCombineForm.setUser_token(form.getUser_token());
            SubscriptionCombineQryBackVo subscriptionCombineQryBackVo = ledT2Service.func_comb_subscription(subscriptionCombineForm);
            if(!VOUtil.isSuccess(subscriptionCombineQryBackVo)){
                return VOUtil.genBGErrorResult(subscriptionCombineQryBackVo.getError_no(),subscriptionCombineQryBackVo.getError_info(),HSBCPSBackVO.class);
            }
            if(subscriptionCombineQryBackVo.getData_list()==null || subscriptionCombineQryBackVo.getData_list().size() == 0){
                return VOUtil.genErrorResult(HSBCPSErrorCodes.PushErrorInfo.XINAN_PUSH_SUBSCRIPTION_EMPTY,PushBack.class);
            }
            boolean com_exist = false;
            for(SubscriptionCombineResult result : subscriptionCombineQryBackVo.getData_list()){
                if(result.getCombi_code().equals(form.getCombi_code())){
                    com_exist = true;
                }
            }
            if(!com_exist){
                return VOUtil.genErrorResult(HSBCPSErrorCodes.PushErrorInfo.XINAN_PUSH_SUBSCRIPTION_ERR,PushBack.class);
            }
            qry.setTag(form.getCombi_code());
        }
        UserRemind userRemind = userRemindService.getRemind(qry);
        if(userRemind == null){
            qry.setFlag(form.getFlag());
            userRemindService.addRemind(qry);
            return VOUtil.setSuccessResult(new HSBCPSBackVO());
        }
        userRemind.setFlag(form.getFlag());
        userRemindService.updateRemindFlag(userRemind);
        return VOUtil.setSuccessResult(new HSBCPSBackVO());
    }

    @Override
    public HSBCPSBackVO save_all_remind(AllRemindSaveForm form) {
        UserRemind userRemind = new UserRemind();
        userRemind.setCompany_id(form.getCompany_id());
        userRemind.setUser_id(form.getUser().getUser_id());
        userRemind.setRemind_type(UserRemindType.COMBINE.getVal());
        UserRemind temp = userRemindService.getRemind(userRemind);
        if(temp == null){
            userRemind.setFlag(form.getFlag());
            userRemindService.addRemind(userRemind);
            return VOUtil.setSuccessResult(new HSBCPSBackVO());
        }
        temp.setFlag(form.getFlag());
        userRemindService.updateRemindFlag(temp);
        return VOUtil.setSuccessResult(new HSBCPSBackVO());
    }

}
